﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Web.Script.Serialization;

namespace JsValidator
{
    public class Config
    {
        public List<string> Inputs { get; set; }
        public List<string> Externs { get; set; }
        public string CompilationLevel { get; set; }
        public string WarningLevel { get; set; }
        public string OutputFile { get; set; }
        public bool TreatNoJavaAsWarning { get; set; }
        public string Ccargs { get; set; }
        public bool IgnoreAllWarnings { get; set; }

        /// <summary>
        /// list of [regexp for] warnings that should be treated as errors
        /// </summary>
        public List<string> Warnings { get; set; }

        /// <summary>
        /// list of [regexp for] warnings that should be completely hidden
        /// </summary>
        public List<string> IgnoreWarnings { get; set; }

        /// <summary>
        /// list of [regexp for] errors that should be completely hidden
        /// </summary>
        public List<string> IgnoreErrors { get; set; }

        public static Config Deserialize(string json)
        {
            var serializer = new JavaScriptSerializer();
            var foo = serializer.Deserialize<Config>(json);

            foo.CheckDefaults();
            return foo;
        }

        private void CheckDefaults()
        {
            OutputFile = String.IsNullOrEmpty(OutputFile) ? Path.Combine(Path.GetTempPath(), "compiled.js") : OutputFile;
            Ccargs = Ccargs ?? "";
            Warnings = Warnings ?? new List<string>();
            Inputs = Inputs ?? new List<string>();
            Externs = Externs ?? new List<string>();
            IgnoreWarnings = IgnoreWarnings ?? new List<string>();
            IgnoreErrors = IgnoreErrors ?? new List<string>();


            CompilationLevel = CompilationLevel ?? "ADVANCED_OPTIMIZATIONS";
            WarningLevel = WarningLevel ?? "VERBOSE";


        }
    }
}